﻿
/*******************************************************************************************
// ©2009 Microsoft Corporation.  This code is provided under the Microsoft Public License.
*******************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Diagnostics;

namespace UCMACellularWS
{
    public class PubSubLogger
    {
        private static StringBuilder _eventLogMessages = new StringBuilder();
        public void Log(string message)
        {
            string timeStamp = System.DateTime.Now.ToString("hh:mm:ss.ff");
            message = message + " : " + timeStamp;
            Console.WriteLine(message);
            System.Diagnostics.Debug.WriteLine(message);
            _eventLogMessages.AppendLine(message);
        }

        public void Log(string message, EventLogEntryType eventType)
        {
            if (!System.Diagnostics.EventLog.SourceExists("PubSub"))
                System.Diagnostics.EventLog.CreateEventSource(
                   "PubSub", "Application");
            EventLog EventLog1 = new EventLog();
            EventLog1.Source = "PubSub";

            EventLog1.WriteEntry(message, eventType);
        }

        public void Log(Exception ex)
        {
            Log(ex.ToString(), EventLogEntryType.Error);
        }

        public void Log(string message, Exception ex)
        {
            Log(message + ": " + ex.Message);
        }

        public void ShutDown()
        {
            //ToDo: Nice to Have - If the string builder gets larger than the max length of a eventlog entry,
            //split it into multiple event log entries.
            _eventLogMessages.AppendLine("End of Log");
            Log(_eventLogMessages.ToString(), EventLogEntryType.Information);
        }
    }
}
